package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingReimburseLevelSettingDetailEntity;
import com.ly.heart.domain.KingReimburseLevelSettingEntity;
import com.ly.heart.domain.dto.KingReimburseLevelSettingDto;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingReimburseLevelSettingMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingReimburseLevelSettingDetailService;
import com.ly.heart.service.IKingReimburseLevelSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 员工报销级别设置 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-18
 */
@Service
public class KingReimburseLevelSettingServiceImpl extends ServiceImpl<KingReimburseLevelSettingMapper, KingReimburseLevelSettingEntity> implements IKingReimburseLevelSettingService {

    @Resource
    private IActModelService iActModelService;
    @Resource
    private IKingReimburseLevelSettingDetailService iKingReimburseLevelSettingDetailService;

    @Override
    public List<KingReimburseLevelSettingEntity> queryList (KingReimburseLevelSettingEntity kingReimburseLevelSettingEntity) {
        return null;
    }

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AjaxResult draft (KingReimburseLevelSettingDto kingReimburseLevelSettingDto) {
        KingReimburseLevelSettingEntity kingReimburseLevelSettingEntity = kingReimburseLevelSettingDto.getKingReimburseLevelSettingEntity ();
        kingReimburseLevelSettingEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingReimburseLevelSettingEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingReimburseLevelSettingEntity.setCreateTime ( DateUtils.getNowDate () );
        kingReimburseLevelSettingEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        updateReimburseLevelSettingDetail ( kingReimburseLevelSettingDto.getKingReimburseLevelSettingDetailEntities (), kingReimburseLevelSettingEntity.getId () );
        boolean save = this.save ( kingReimburseLevelSettingEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存员工报销级别设置失败！" );
        }
        return AjaxResult.success ( "暂存员工报销级别设置成功", kingReimburseLevelSettingEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingReimburseLevelSettingEntity kingReimburseLevelSettingEntity) {
        if ( StringUtils.isBlank ( kingReimburseLevelSettingEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingReimburseLevelSettingEntity.getId () ).
                setBusinessType ( kingReimburseLevelSettingEntity.getBusinessType () ).setModelId ( kingReimburseLevelSettingEntity.getModelId () ).
                setBusinessName ( "员工报销级别设置".concat ( kingReimburseLevelSettingEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingReimburseLevelSettingEntity> ().
                set ( KingReimburseLevelSettingEntity :: getInstanceId, instanceId ).
                set ( KingReimburseLevelSettingEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                eq ( KingReimburseLevelSettingEntity :: getId, kingReimburseLevelSettingEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交员工报销级别设置失败！" );
        }
        return AjaxResult.success ( "提交员工报销级别设置成功" );
    }

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AjaxResult updateSingle (KingReimburseLevelSettingDto kingReimburseLevelSettingDto) {
        KingReimburseLevelSettingEntity kingReimburseLevelSettingEntity = kingReimburseLevelSettingDto.getKingReimburseLevelSettingEntity ();
        kingReimburseLevelSettingEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingReimburseLevelSettingEntity.setUpdateTime ( DateUtils.getNowDate () );
        updateReimburseLevelSettingDetail ( kingReimburseLevelSettingDto.getKingReimburseLevelSettingDetailEntities (), kingReimburseLevelSettingEntity.getId () );
        boolean b = this.updateById ( kingReimburseLevelSettingEntity );
        if ( ! b ) {
            throw new CustomException ( "修改员工报销级别设置失败！" );
        }
        return AjaxResult.success ( "修改员工报销级别设置成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingReimburseLevelSettingEntity> ().
                set ( KingReimburseLevelSettingEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingReimburseLevelSettingEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingReimburseLevelSettingEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingReimburseLevelSettingEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除员工报销级别设置失败！" );
        }
        return AjaxResult.success ( "删除员工报销级别设置成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingReimburseLevelSettingEntity reimburseLevelSettingEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingReimburseLevelSettingEntity> ().
                set ( KingReimburseLevelSettingEntity :: getIsDisabled, reimburseLevelSettingEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingReimburseLevelSettingEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingReimburseLevelSettingEntity :: getDisabledTime, DateUtils.getNowDate () ).
                eq ( KingReimburseLevelSettingEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( reimburseLevelSettingEntity.getIsDisabled () == 1 ? "禁用员工报销级别设置失败！" : "解禁员工报销级别设置失败！" );
        }
        return AjaxResult.success ( reimburseLevelSettingEntity.getIsDisabled () == 1 ? "禁用员工报销级别设置成功" : "解禁员工报销级别设置成功" );
    }

    private void updateReimburseLevelSettingDetail (List<KingReimburseLevelSettingDetailEntity> kingReimburseLevelSettingDetailEntities, String reimburseId) {
        if ( ! CollectionUtils.isEmpty ( kingReimburseLevelSettingDetailEntities ) ) {
            iKingReimburseLevelSettingDetailService.remove ( new LambdaUpdateWrapper<KingReimburseLevelSettingDetailEntity> ().eq ( KingReimburseLevelSettingDetailEntity :: getReimburseLevelSettingId, reimburseId ) );
            kingReimburseLevelSettingDetailEntities.forEach ( detail -> {
                detail.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                detail.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                boolean save = iKingReimburseLevelSettingDetailService.save ( detail );
                if ( ! save ) {
                    throw new CustomException ( "更新员工报销级别设置明细失败！" );
                }
            } );
        }
    }

}
